home *** CD-ROM | disk | FTP | other *** search
- /*************************************************************************************
- *
- * PDP-8 Simulation Program- code loader
- *
- * ©1992 Graham Cox. All Rights Reserved.
- *
- * Modification History:
- * 9/3/92 created from scratch.
- *
- *
- *
- *************************************************************************************/
-
- #include "PDPGlobalEqu.p"
- #include "Global.h"
- #include "EditGlobalEqu.p"
-
- #include "PDPLoader.proto.h"
-
- PDPMemHdl GetMemory(WindowPtr theWindow);
- ObjectPtr TranslateBoolToCoxy(oHandle object);
- textEdHdl GetWEditRecord(WindowPtr theWindow);
- WindowPtr GetProcessWindow(int processID);
- char IsEditKind(WindowPtr);
-
- int TargetProcessID =1; /* global target process ID for assemble command */
-
- int LoadProcessMemory(WindowPtr targetProcess,ObjectPtr codeFile)
- {
- /* this procedure implements the loader. The file pointed to (previously loaded if on
- disk) is copied into memory at the address specified in the file. The memory is
- the simulated process memory associated with the target window. The function
- returns an error code to indicate success (0 is no error) */
-
- PDPMemHdl TargetMem;
- int cOrigin,cWords,cMaxWords;
- PDPWordPtr sourceWord,destWord;
-
- if (IsSimulator(targetProcess)) {
- if (codeFile != NIL && codeFile->version ==1) {
- TargetMem = GetMemory(targetProcess);
-
- if (TargetMem != NIL) {
-
- /* OK- we're in business. Decode the origin address, etc */
-
- cOrigin = codeFile->codeOrigin;
- cMaxWords = codeFile->cwLength;
-
- if (cOrigin + cMaxWords < 4095) {
-
- /* code will fit into memory OK, so let's load it */
-
- sourceWord = &codeFile->firstWord;
- HLock((Handle) TargetMem);
- destWord = (PDPWordPtr) *TargetMem;
- destWord += cOrigin;
-
- for (cWords =0;cWords<cMaxWords;cWords++) {
- *destWord = *sourceWord;
- sourceWord++;
- destWord++;
- }
-
- HUnlock((Handle) TargetMem);
- return(noErr);
- }
- else
- return(CodeFileTooLong);
- }
- else
- return(TargetHasNoMemory);
- }
- else
- return(CodeFileBadVersion);
- }
- else
- return(TargetNotSimulator);
- }
-
-
- ObjectPtr MakeDummyCodeFile(void)
- {
- /* creates space for a file, sets up the header, and fills it with random data. This
- can then be saved as a dummy file or loaded into a process. */
-
- ObjectPtr theFile;
- PDPWord theWord;
- int wCount;
- PDPWordPtr destPtr;
-
- theFile = NewPtr(sizeof(ObjectHeader) + (sizeof(PDPWord) * 1000));
- if (theFile != NIL) {
- theFile->version = 1;
- theFile->codeOrigin = 5;
- theFile->cwLength = 1001;
- theFile->reserved = 'test';
-
- destPtr = &theFile->firstWord;
-
- for(wCount =0;wCount<1001;wCount++) {
- *destPtr = Random() & 0x0FFF;
- destPtr++;
- }
- }
-
- return(theFile);
- }
-
-
- PDPLoad(WindowPtr theWindow,SFReply *theReply)
- {
- /* High-Level call to install object code into window process from file */
-
- ObjectPtr theCode;
- int theErr,fPath;
- long fBytes;
-
- if (theReply->good && IsSimulator(theWindow)) {
- if (! FSOpen(&theReply->fName,theReply->vRefNum,&fPath)) {
- if (! GetEOF(fPath,&fBytes)) {
-
- theCode = (ObjectPtr) NewPtr(fBytes);
- if (theCode != NIL) {
- if (! FSRead(fPath,&fBytes,(Ptr)theCode)) {
-
- theErr = LoadProcessMemory(theWindow,theCode);
- if (theErr)
- ShowProcessError(theErr);
- MarkForUpdate(theWindow);
- }
- DisposPtr(theCode);
- }
- }
- theErr = FSClose(fPath);
- }
- }
- }
-
-
- ClearProcessMem(WindowPtr theWindow)
- {
- /* sets all locations in simulated memory to zero */
-
- PDPMemHdl theMemory;
- PDPMemPtr mPtr;
- PDPWord index;
-
- if (IsSimulator(theWindow)) {
- theMemory = GetMemory(theWindow);
-
- if (theMemory != NIL) {
- HLock((Handle) theMemory);
- for (index = 0; index <4096;index++) {
- mPtr = *theMemory;
- mPtr[index] = 0;
- }
- HUnlock((Handle) theMemory);
- }
- }
- }
-
-
- ChooseTextFile(SFReply *theReply)
- {
- /* presents SF Open dialog box for text files */
-
- SFTypeList theList;
- Point where;
-
- theList[0] = 'TEXT';
- SetPt(&where,100,100);
- SFDialogLocation(getDlgID,&where);
- SFGetFile(where,(ConstStr255Param)'/p',NIL,2,&theList,NIL,theReply);
- }
-
-
- #define ProgressDialogID 515
-
-
- PDPAssembleCode(WindowPtr theWindow)
- {
- /* incredibly high level call to implement the assemble command */
-
- extern int AssemblyDest;
- SFReply tFile;
- pHandle theProgram;
- oHandle theObject;
- ObjectPtr coxyFormat;
- int theErr;
- StringHandle saveFName;
- DialogPtr progressBox;
- Rect dRect;
- long dWait,lineNum;
- textEdHdl asmText;
-
- if (IsEditKind(theWindow)) {
- asmText = GetWEditRecord(theWindow);
- if (asmText != NIL)
- tFile = (*asmText)->asmFile;
- else
- ChooseTextFile(&tFile);
- if (tFile.good) {
- AsmError = noErr;
- SetHiliteLine(theWindow,NIL);
-
- progressBox = GetNewDialog(ProgressDialogID,NIL,(WindowPtr)-1L);
-
- if (progressBox != NIL) {
-
- theProgram = newProgram();
-
- if (theProgram != NIL) {
- ParamText("\p",&tFile.fName,"\p1","\p");
- dRect = progressBox->portRect;
- MoveWindow(progressBox,hCentre(&dRect),GetMBarHeight()+10,TRUE);
- ShowWindow(progressBox);
- DrawDialog(progressBox);
-
- (*(*theProgram)->sourceCode)->fileSpec = tFile;
-
- loadSource((*theProgram)->sourceCode);
- if (!AsmError) {
-
- pass(theProgram,1);
-
- if (! AsmError) {
-
- createObjectSpace((*theProgram)->objectCode);
-
- if (!AsmError) {
- Delay(40,&dWait);
- ParamText("\p",&tFile.fName,"\p2","\p");
- DrawDialog(progressBox);
-
- pass(theProgram,2);
- Delay(60,&dWait);
-
- if (!AsmError) {
- coxyFormat = TranslateBoolToCoxy((*theProgram)->objectCode);
- }
- }
- }
- }
- DisposDialog(progressBox);
- lineNum = (*(*theProgram)->sourceCode)->line;
- disposeProgram(theProgram);
-
- if (AsmError) {
- AssemblyError(AsmError,lineNum);
- SetHiliteLine(theWindow,lineNum);
- }
- else {
- AssemblyError(CompletedOK,-1L);
-
-
- if (AssemblyDest == toMemory) {
- theErr = LoadProcessMemory(GetProcessWindow(TargetProcessID),coxyFormat);
- if (theErr)
- AssemblyError(theErr,-1L);
- else
- MarkForUpdate(GetProcessWindow(TargetProcessID));
- }
- else {
- saveFName = NewString(&tFile.fName);
- if (saveFName != NIL) {
- MakeSaveName(saveFName);
- NameObjectFile(saveFName,&tFile);
- DisposHandle(saveFName);
- SaveObjectFile(&tFile,coxyFormat);
- }
- }
- DisposPtr(coxyFormat);
- }
- MarkForUpdate(theWindow);
- }
- else
- AssemblyError(NotEnoughMemory,-1L);
- }
- }
- }
- }
-
-
- ObjectPtr TranslateBoolToCoxy(oHandle object)
- {
- /* translates Adrians block of object stuff to my type, complete with header! WOW! */
-
- ObjectPtr temp;
- long size;
- Ptr s1,s2;
-
- size = (*object)->size;
- temp = (ObjectPtr) NewPtr(sizeof(ObjectHeader) + size * sizeof(wordType));
- if (temp != NIL) {
- temp->version = 1;
- temp->codeOrigin = (*object)->startAddress;
- temp->cwLength = size;
- temp->reserved = 0;
-
- HLock((Handle) (*object)->storage);
- s1 = *(*object)->storage;
- s2 = &temp->firstWord;
-
- BlockMove(s1,s2,size * sizeof(wordType));
- HUnlock((Handle) (*object)->storage);
- }
-
- return(temp);
- }
-
- #define ErrorStringsID 129
-
- AssemblyError(int errorCode,long lineNum)
- {
- /* put up alert box with error message reporting assembly errors. If lineNum = -1
- the line number message is not included */
-
- int aHit;
- Str32 lineStr,valStr;
- Str255 message;
-
- if (errorCode) {
- if (lineNum >= 0)
- GetIndString(&lineStr,standardResID,11);
- else
- CopyString("\p",&lineStr);
- NumToString(lineNum,&valStr);
- GetIndString(&message,ErrorStringsID,-errorCode-176);
- ParamText(&message,&lineStr,&valStr,"\p");
- aHit = xAlert(129,NIL);
- }
- }
-
-
- MakeSaveName(StringHandle theString)
- {
- /* makes a suitable name for saving the object file as. This is the same as the
- source file, less any .extension, appended with .OBJ */
-
- Ptr s1,s2;
- long L1,L2,mResult;
- Str32 targ,replace;
-
- CopyString("\p.OBJ",&replace);
- CopyString("\p.SRC",&targ);
- s1 = &targ[1];
- s2 = &replace[1];
-
- mResult = Munger(theString,1,s1,4,s2,4);
- if (mResult < 0) {
- /* we failed to find .SRC, so just append the extension */
- L1 = **theString;
- mResult = Munger(theString,L1+1,NIL,0,s2,4);
- **theString = L1+4;
- }
- }
-
-
- NameObjectFile(StringHandle defaultName,SFReply *theReply)
- {
- /* presents SF Save dialog Box for saving object file */
-
- Point where;
-
- SetPt(&where,100,100);
- SFDialogLocation(putDlgID,&where);
- HLock((Handle) defaultName);
- SFPutFile(where,NIL,*defaultName,NIL,theReply);
- HUnlock((Handle) defaultName);
- }
-
-
- SaveObjectFile(SFReply *fSpec,ObjectPtr theCode)
- {
- /* writes out the object file to the disk if you're lucky */
-
- OSErr theErr;
- int pathRef;
- long bytes;
-
- if (fSpec->good) {
- theErr = Create(&fSpec->fName,fSpec->vRefNum,'PDP8','OBJF');
- if (theErr == noErr || theErr == dupFNErr) {
- if (! FSOpen(&fSpec->fName,fSpec->vRefNum,&pathRef)) {
- bytes = GetPtrSize(theCode);
-
- theErr = FSWrite(pathRef,&bytes,theCode);
-
- if (theErr)
- SysBeep(1);
-
- theErr = FSClose(pathRef);
- }
- }
- }
- }
-
-
- #define SetTargetDialogID 516
- #define STItemEdit 3
-
-
- int SetTargetProcess(long currentProcess)
- {
- /* displays dialog box to set the target process ID */
-
- DialogPtr theDialog;
- int theItem,itemType;
- Handle itemHand;
- Rect itemBox;
- Str32 edStr;
- long tempProcID;
-
- theDialog = GetNewDialog(SetTargetDialogID,NIL,(WindowPtr)-1L);
- if (theDialog != NIL) {
- PosDialog(theDialog);
- OutlineDItem(theDialog,ok);
-
- GetDItem(theDialog,STItemEdit,&itemType,&itemHand,&itemBox);
- NumToString(currentProcess,&edStr);
- SetIText(itemHand,&edStr);
- SelIText(theDialog,STItemEdit,0,32767);
- theItem = 0;
- while (theItem == 0) {
- ModalDialog(NIL,&theItem);
- switch (theItem) {
- case ok:
- GetDItem(theDialog,STItemEdit,&itemType,&itemHand,&itemBox);
- GetIText(itemHand,&edStr);
- StringToNum(&edStr,&tempProcID);
- if (tempProcID <= 0) {
- theItem = 0;
- SysBeep(1);
- SelIText(theDialog,STItemEdit,0,32767);
- break;
- }
- else
- currentProcess = tempProcID;
- case cancel:
- break;
- default:
- theItem = 0;
- break;
- }
- }
- DisposDialog(theDialog);
- return(LoWord(currentProcess));
- }
- }
-
-
- WindowPtr GetProcessWindow(int processID)
- {
- /* searches for a process window with the given ID. If available, returns the window,
- otherwise returns NIL. processID can't be zero, or an invalid window is returned */
-
- WindowPeek temp;
- int wProcess;
-
- temp = FrontWindow();
- while (temp != NIL) {
- wProcess = GetProcessID(temp);
- if (wProcess == processID)
- break;
- else
- temp = temp->nextWindow;
- }
- return(temp);
- }
-
-
-